﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
//using System.NET;
using System.Net.Sockets;
using System.IO;
using System.Net;
namespace PrinteMaster
{
    //1065206989
    class Network
    {
        private Thread th;
        private TcpListener tcpLis;
        private bool bLisRun = true;
        private NetworkStream ns;
        private StreamWriter sw;
        private TcpClient tcpCli;
        private Socket sk;
        public void create()
        {
            try
            {
                th = new Thread(new ThreadStart(Listen));
                th.Start();
                tcpCli = new TcpClient("localhost",8080);
                //向远程计算机提出连接申请
                ns = tcpCli.GetStream();
                //如果连接申请建立，则获得用以传送数据的数据流

            }
            catch (Exception e)
            {   
               
                Console.Write(e.Message);
            }
        }

        private void Listen()
        {
            try
            {
                byte[] ip = new byte[4];
                ip[0] = 127;
                ip[1] = 0;
                ip[2] = 0;
                ip[3] = 1;
                IPAddress ipAdd = new IPAddress(ip);
                tcpLis = new TcpListener(ipAdd,8080);
                tcpLis.Start();
                //侦听指定端口号
                //txtShowMsg.Text = "正在监听...";
                //接受远程计算机的连接请求，并获得用以接收数据的Socket实例
                sk = tcpLis.AcceptSocket();
                //获得远程计算机对应的网络远程终结点
                EndPoint tempRemoteEP = sk.RemoteEndPoint;
                IPEndPoint tempRemoteIP = (IPEndPoint)tempRemoteEP;

                IPHostEntry host = Dns.GetHostEntry(ipAdd);
                string HostName = host.HostName;
                //根据获得的远程计算机对应的网络远程终结点获得远程计算机的名称
                //txtShowMsg.Text = "'" + HostName + "' " + "远程计算机正确连接！";
                //循环侦听
                while (bLisRun)
                {
                    Byte[] stream = new Byte[80];
                    //定义从远程计算机接收到数据存放的数据缓冲区
                    string time = DateTime.Now.ToString();
                    //获得当前的时间
                    int i = sk.ReceiveFrom(stream, ref tempRemoteEP);
                    //接收数据，并存放到定义的缓冲区中
                    string sMessage = System.Text.Encoding.UTF8.
                    GetString(stream);
                    //以指定的编码，从缓冲区中解析出内容
                    //listBox2.Items.Add(time + "" + HostName + ":");
                    //listBox2.Items.Add(sMessage);
                    //显示接收到的数据
                    Console.WriteLine(sMessage);
                }
            }
            catch (System.Security.SecurityException)
            {
                MessageBox.Show("防火墙安全错误！", "错误",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        public void destroy()
        {
            try
            {
                bLisRun = false;
                th.Abort();
                th = null;
                tcpLis.Stop();
                sk.Close();
                tcpCli.Close();
            }
            catch
            {
            }   //以上程序为手工添加，下面为新建工程是系统生成； 
        }

        public void send(string msg)
        {
             
            try
            {
                string sMsg = msg;
                string MyName = Dns.GetHostName();
                //以特定的编码往向数据流中写入数据,
                //默认为UTF8Encoding 的实例
                sw = new StreamWriter(ns);
                //将字符串写入数据流中
                sw.Write(sMsg);
                //清理当前编写器的所有缓冲区，并使所有缓冲数据写入基础流
                sw.Flush();
                string time = DateTime.Now.ToString();
                //显示传送的数据和时间

            }
            //异常处理
            catch (Exception)
            {
                Console.Write("无法发送信息到目标计算机！");
            }
        }
    }
}
